<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
      <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
      <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="i-ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button
          >批量操作
          <Icon icon="i-mdi:chevron-down" />
        </a-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>

    <template #qrCodeUrl="{ record }">
      <ImagePreview :image-list="[{ src: record.qrCodeUrl, width: 80 }]" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <MpAccountDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <MpAccountDesc @register="registerDesc" />
</template>
<script lang="ts" name="weixin-mp-account" setup>
  import { ref } from "vue"
  import { ImagePreview } from "/@/components/Preview/index"
  import { ImageProps } from "/@/components/Preview/src/typing"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useModal } from "/@/components/Modal"
  import MpAccountDrawer from "./MpAccountDrawer.vue"
  import MpAccountDesc from "./MpAccountDesc.vue"
  import { columns, searchFormSchema } from "./data"
  import { pageMpAccount, deleteMpAccount, batchDeleteMpAccount, getExportUrl, getImportUrl } from "./api"
  import { useListPage } from "/@/hooks/system/useListPage"
  const showFooter = ref(true)
  const [registerDrawer, { openDrawer }] = useDrawer()
  const [registerDesc, { openDrawer: openMpAccountDesc }] = useDrawer()

  // 列表页面公共参数、方法
  const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
    designScope: "weixin-mp-account",
    tableProps: {
      title: "微信公众号账号列表",
      api: pageMpAccount,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 120,
      },
      rowSelection: null,
    },
    exportConfig: {
      name: "微信公众号账号列表",
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  })
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

  /**
   * 新增
   */
  function handleCreate() {
    showFooter.value = true
    openDrawer(true, {
      isUpdate: false,
    })
  }
  /**
   * 编辑
   */
  function handleEdit(record: Recordable) {
    showFooter.value = true
    openDrawer(true, {
      record,
      isUpdate: true,
    })
  }
  /**
   * 详情
   */
  function handleDetail(record) {
    showFooter.value = false
    openMpAccountDesc(true, {
      record,
      isUpdate: true,
    })
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteMpAccount({ id: record.id }, reload)
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteMpAccount({ ids: selectedRowKeys.value }, reload)
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "详情",
        onClick: handleDetail.bind(null, record),
      },
    ]
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    let resp: any = [
      {
        label: "删除",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
    return resp
  }
</script>

<style lang="less"></style>
